package com.loong.mapper.impl;

import com.loong.mapper.OrderMapper;
import com.loong.pojo.Item;
import com.loong.pojo.Orders;
import com.loong.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.List;

public class OrderMapperImpl implements OrderMapper {
    Logger logger = Logger.getLogger(OrderMapperImpl.class);
    @Override
    public void insertOrders(Orders orders) {
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()){
            OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
            mapper.insertOrders(orders);
            sqlSession.commit();
        }catch (Exception e){
            logger.error("Error inserting order: ", e);
            logger.error(e.getMessage());
        }
    }

    @Override
    public List<Orders> selectOrderByUid(Integer uid) {
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()){
            OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
            List<Orders> orders = mapper.selectOrderByUid(uid);
            return orders;
        }catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    @Override
    public Orders selectOrderByOid(String oid) {
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()){
            OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
            Orders orders = mapper.selectOrderByOid(oid);
            return orders;
        }catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    @Override
    public void updateStateByOid(String oid) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
            mapper.updateStateByOid(oid);
            sqlSession.commit();
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    @Override
    public void updateStateToFourByOid(String oid) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
            mapper.updateStateToFourByOid(oid);
            sqlSession.commit();
        }catch (Exception e){
            logger.error(e.getMessage());
        }
    }

}
